from student import Student
from teacher import Teacher
import os

def clear_screen():
    os.system('cls' if os.name == 'nt' else 'clear')

def teacher_menu():
    teacher = Teacher()
    while True:
        clear_screen()
        print("\n" + "="*20 + " 教师系统 " + "="*20)
        print("1. 生成新试卷")
        print("2. 查看所有试卷")
        print("3. 批改答卷")
        print("4. 返回主菜单")
        print("="*50)
        
        choice = input("\n请选择操作：")
        
        if choice == '1':
            exam_id = input("\n请输入试卷ID（如: test01）：")
            count = int(input("请输入题目数量（默认30题）：") or "30")
            exam_id = teacher.generate_exam(exam_id, count)
            
            # 读取试卷并显示
            exam = teacher.file_handler.load_exam(exam_id)
            print(teacher.format_exam_questions(exam['questions']))
            print(f"\n试卷已生成，ID为：{exam_id}")
            input("\n按回车继续...")
            
        elif choice == '2':
            exams = teacher.file_handler.list_exams()
            print("\n" + "="*20 + " 可用试卷列表 " + "="*20)
            if exams:
                for exam_id in exams:
                    print(f"试卷ID：{exam_id}")
            else:
                print("当前没有可用的试卷")
            print("="*50)
            input("\n按回车继续...")
            
        elif choice == '3':
            student_id = input("\n请输入学生学号：")
            exam_id = input("请输入试卷ID：")
            submission_id = f"{student_id}_{exam_id}"
            try:
                score, results, wrong_questions = teacher.grade_submission(submission_id)
                print("\n" + "="*20 + " 批改结果 " + "="*20)
                print(f"学号：{student_id}")
                print(f"试卷：{exam_id}")
                print(f"得分：{score}分")
                if wrong_questions:
                    print("\n错题分析：")
                    print("-"*50)
                    for wrong in wrong_questions:
                        print(f"{wrong['question']}")
                        print(f"学生答案：{wrong['student_answer']}")
                        print(f"正确答案：{wrong['correct_answer']}")
                        print("-"*50)
                print("="*50)
                input("\n按回车继续...")
            except Exception as e:
                print(f"\n批改失败：{str(e)}")
                input("\n按回车继续...")
                
        elif choice == '4':
            break

def student_menu():
    name = input("\n请输入你的名字：")
    student_id = input("请输入你的学号：")
    student = Student(name, student_id)
    teacher = Teacher()  # 用于查看试卷列表
    
    while True:
        clear_screen()
        print("\n" + "="*20 + f" 学生系统 " + "="*20)
        print(f"姓名：{name}")
        print(f"学号：{student_id}")
        print("-"*50)
        print("1. 查看可用试卷")
        print("2. 开始答题")
        print("3. 查看成绩和错题分析")
        print("4. 返回主菜单")
        print("="*50)
        
        choice = input("\n请选择操作：")
        
        if choice == '1':
            exams = teacher.file_handler.list_exams()
            print("\n" + "="*20 + " 可用试卷 " + "="*20)
            if exams:
                for exam_id in exams:
                    print(f"试卷ID：{exam_id}")
            else:
                print("当前没有可用的试卷")
            print("="*50)
            input("\n按回车继续...")
            
        elif choice == '2':
            exam_id = input("\n请输入要答题的试卷ID：")
            try:
                submission_id = student.take_exam(exam_id)
                print("\n" + "="*20 + " 答题完成 " + "="*20)
                print(f"你的答卷ID为：{submission_id}")
                print("请记住此ID以便查询成绩和错题分析")
                print("="*50)
                input("\n按回车继续...")
            except Exception as e:
                print(f"\n答题失败：{str(e)}")
                input("\n按回车继续...")
                
        elif choice == '3':
            exam_id = input("\n请输入试卷ID：")
            submission_id = f"{student_id}_{exam_id}"
            try:
                analysis = student.view_wrong_questions(submission_id)
                if analysis:
                    print("\n" + "="*20 + " 成绩分析 " + "="*20)
                    print(f"学号：{student_id}")
                    print(f"姓名：{name}")
                    print(f"试卷：{exam_id}")
                    print(f"总题数：{analysis['total_questions']}")
                    print(f"得分：{analysis['score']}分")
                    if analysis['wrong_questions']:
                        print("\n错题分析：")
                        print("-"*50)
                        for wrong in analysis['wrong_questions']:
                            print(f"{wrong['question']}")
                            print(f"你的答案：{wrong['student_answer']}")
                            print(f"正确答案：{wrong['correct_answer']}")
                            print("-"*50)
                    else:
                        print("\n恭喜！你没有错题！")
                else:
                    print("\n该答卷尚未批改！")
                print("="*50)
                input("\n按回车继续...")
            except Exception as e:
                print(f"\n查看失败：{str(e)}")
                input("\n按回车继续...")
                
        elif choice == '4':
            break

def main():
    while True:
        clear_screen()
        print("\n" + "="*20 + " 数学答题系统 " + "="*20)
        print("1. 教师入口")
        print("2. 学生入口")
        print("3. 退出系统")
        print("="*50)
        
        choice = input("\n请选择角色：")
        
        if choice == '1':
            teacher_menu()
        elif choice == '2':
            student_menu()
        elif choice == '3':
            print("\n感谢使用！再见！")
            break

if __name__ == "__main__":
    main()